home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / emulator / bsvc-1.000 / bsvc-1 / bsvc-1.0.4 / src / Assemblers / hecasm / emit.c < prev    next >
C/C++ Source or Header  |  1995-07-26  |  3KB  |  167 lines

  1. /* this routine is paired with lex.c.  They do arithmetic expression */
  2. /* analysis in agreement with operator precedence.  The general idea */
  3. /* is that expressions are tokenized and converted to post fix and */
  4. /* placed in a buffer until the entire expression is there.  Then it */
  5. /* is evaluated and the result returned */
  6.  
  7. #include "asm.h"
  8. #include "global.h"
  9.  
  10. /* place the token from the lexer in a buffer */
  11. emit(t)
  12. int t;
  13. {
  14.  
  15. switch(t)
  16.     {
  17.     case NUM:
  18.     /* integer */
  19.         opq[optop++] = token;
  20.         break;
  21.     case ID:
  22.     /* symbol */
  23.         opq[optop++] = token;
  24.         break;
  25.     default: 
  26.     /* an operator */
  27.         opq[optop].type = NONE;
  28.         opq[optop++].uval.ival = t;
  29.         break;
  30.     }
  31. }
  32.  
  33.  
  34. /* evaluate the post fix expression stored in the buffer */
  35. /* this is called when the expression is collected */
  36. post()
  37. {
  38. int i;
  39.  
  40. /* proceed down the buffer */
  41. for(i=0;i<optop;i++)
  42.     {
  43.     /* if it is an integer push its value on a stack */
  44.     if (opq[i].type == NUM)
  45.         push(opq[i].uval.ival);
  46.  
  47.     /* if it is a variable lookup its value and push it on the stack */
  48.     else if (opq[i].type == ID)
  49.     {
  50.         if (oklookup(&opq[i]))
  51.             push(opq[i].uval.sval->s_value);
  52.         else
  53.         /* symbol is undefined */
  54.             {
  55.             push(0);
  56.             undefined = TRUE;
  57.             }
  58.     }
  59.     else
  60.     /* it is an operator so evaluate some of the stack */
  61.         perform(opq[i].uval.ival);
  62.     }
  63.  
  64. /* pop the last value off the stack and return it */
  65. pop(&i);
  66. return(i);
  67. }
  68.  
  69.  
  70. /* see if the symbol is defined */
  71. oklookup(ptr)
  72. vtype *ptr;
  73. {
  74.  
  75. if (ptr->uval.sval->s_type & S_VALID)
  76.     return(TRUE);
  77.  
  78. if (ptr->uval.sval->s_type == S_UND)
  79.     err('u',"undefined symbol");
  80. return(FALSE);
  81. }
  82.  
  83.  
  84. /* evaluate an operator */
  85. perform(op)
  86. int op;
  87. {
  88. int v1, v2;
  89.  
  90. /* pop the first argument */
  91. pop(&v2);
  92.  
  93. /* see if it is a single operand operator */
  94. switch(op)
  95.     {
  96.     case NE:
  97.         push(-v2);
  98.         return;
  99.     case CO:
  100.         push(~v2);
  101.         return;
  102.     case NG:
  103.         push(!v2);
  104.         return;
  105.     }
  106.  
  107. /* it must require two operands so pop another */
  108. pop(&v1);
  109.  
  110. switch(op)
  111.     {
  112.     case PL:
  113.         push(v1 + v2);
  114.         break;
  115.     case MI:
  116.         push(v1 - v2);
  117.         break;
  118.     case MU:
  119.         push(v1 * v2);
  120.         break;
  121.     case DI:
  122.         push(v1 / v2);
  123.         break;
  124.     case AN:
  125.         push(v1 & v2);
  126.         break;
  127.     case OR:
  128.         push(v1 | v2);
  129.         break;
  130.     case XO:
  131.         push(v1 ^ v2);
  132.         break;
  133.     default:
  134.         err('u',"undefined operator");
  135.     }
  136. }
  137.  
  138.  
  139. /* evaluation stack and stack pointer */
  140. int stack[STSIZE];
  141. int tos = 0;
  142.  
  143.  
  144. /* push a value on the stack and check for overflow */
  145. push(val)
  146. int val;
  147. {
  148. if (tos == STSIZE)
  149.     {
  150.     err('v',"expression stack overflow");
  151.     exit(0);
  152.     }
  153. stack[tos++] = val;
  154. }
  155.  
  156.  
  157. /* pop a value off the stack and check for underflow */
  158. pop(aval)
  159. int *aval;
  160. {
  161. if (tos == 0)
  162.     err('U',"expression stack underflow");
  163. else
  164.     *aval = stack[--tos];
  165. }
  166.  
  167.